import {
  prop,
  getModelForClass,
  modelOptions,
  index,
} from '@typegoose/typegoose'
import { ApiProperty } from '@nestjs/swagger'
import { IsEmail, IsMobilePhone } from 'class-validator'
import { IdentityCodeValid, Gender } from '../../common/checkIdentity'

@modelOptions({
  schemaOptions: {
    _id: false, //不生成mongoDb默认的id
    toJSON: { virtuals: true }, //显示虚拟属性
    toObject: { virtuals: true },
  },
})
class OtherClass {
  /** 工作性质 */
  //description:描述，required：是否必填,example:示例
  @ApiProperty({ description: '工作性质', required: false, example: '商业' })
  @prop()
  title?: string

  /** 部门 */
  @ApiProperty({
    description: '公司部门',
    required: false,
    example: '销售',
  })
  @prop()
  department?: string

  /** 家庭地址 */
  @ApiProperty({
    description: '家庭地址',
    required: false,
    example: '石家庄***',
  })
  @prop()
  homeAddress?: string

  /** 描述说明*/
  @ApiProperty({ description: '描述', required: false, example: '羽毛球' })
  @prop()
  description?: string

  // @ApiProperty({ description: '年龄', required: false, example: 26 })
  // @prop()
  // age?: number;
  /** 身份证号码 */
  @ApiProperty({
    description: '身份证号',
    required: false,
    example: '130****',
  })
  @prop()
  identity?: string

  /** 年龄 */
  get age() {
    const { pass, age } = IdentityCodeValid(this.identity)
    if (pass) {
      return age
    }
    return NaN
  }

  /** 生日 */
  get birthday() {
    const { pass, birthday } = IdentityCodeValid(this.identity)
    if (pass) {
      return birthday
    }
    return null
  }

  /** 出生地*/
  get birthPlace() {
    const { pass, nativePlace } = IdentityCodeValid(this.identity)
    if (pass) {
      return nativePlace
    }
    return null
  }

  /** 性别 */
  get gender() {
    const { pass, gender } = IdentityCodeValid(this.identity)
    if (pass) {
      return gender
    }
    return Gender.male
  }
}

@index({ busRegistNo: 1 })
@modelOptions({
  schemaOptions: {
    collection: 'employees', //数据库集合的名称
    timestamps: true,
  },
})
export class EmployeeClass {
  @prop({ required: true, unique: true })
  @ApiProperty({
    description: '公司ID',
    example: '123***',
    required: true,
    readOnly: true,
  })
  public companyId: string

  @prop({ required: true, unique: true })
  @ApiProperty({ description: '公司名称', example: '石家庄某某有限公司' })
  public companyName?: string

  /** 雇员姓名 */
  @ApiProperty({ description: '员工姓名', required: true, example: '李**' })
  @prop({ required: true, minlength: 1, maxlength: 30 })
  public name: string

  /** 手机号码 */
  @ApiProperty({ description: '手机号码', required: true })
  @prop({
    required: true, //必须的、必填项
    unique: true, //唯一的
  })
  @IsMobilePhone('zh-CN', {}, { message: '请输入正确的手机号码' })
  mobile!: string

  /** 邮箱号码 */
  @ApiProperty({
    description: '邮箱号码',
    required: true,
    example: '138**@139.com',
  })
  @IsEmail({}, { message: '请输入正确的邮箱号码' })
  @prop({
    required: true,
    unique: true,
  })
  email: string

  /** 其他*/
  @ApiProperty({ description: '其他', required: false })
  @prop()
  other: OtherClass
}

export const EmployeeModel = getModelForClass(EmployeeClass)
